Public Class Desglose

    Private idS As Integer
    Private idE As Integer
    Private objEvaluacion As Evaluacion
    Private data As DataTable

    Public Sub New()

    End Sub

    Public Sub New(ByVal pidS As Integer)
        Me.idS = pidS
    End Sub

    Public Sub New(ByVal pidS As Integer, ByVal pidE As Integer)
        Me.idS = pidS
        Me.idE = pidE
    End Sub

    Public Function obtenerRendimiento() As ArrayList
        Me.obtenerDesglose()
        Dim datos As New ArrayList
        datos.Add(Me.puntajeGeneral)
        datos.Add(Me.puntajeIndividual)
        datos.Add(Me.rendimientoGeneral)
        datos.Add(Me.rendimientoIndividual)
        datos.Add(Me.puntajeGeneralMaximo)
        datos.Add(Me.puntajeIndividualMaximo)
        Return datos
    End Function

    Private Function puntajeGeneral() As ArrayList
        Dim datos As New ArrayList
        Dim valor As Double = 0
        Dim suma As Double = 0
        Dim i As Integer
        For i = 0 To data.Rows.Count - 1
            valor = valor + CType(data.Rows.Item(i).Item("obtenido"), Double)
            suma = suma + CType(data.Rows.Item(i).Item("porcentaje"), Double)
        Next
        datos.Add(valor)
        datos.Add(suma)
        Return datos
    End Function

    Private Function puntajeIndividual() As ArrayList
        Dim datos As New ArrayList
        Dim valor As Double = 0
        Dim suma As Double = 0
        Dim i As Integer
        For i = 0 To data.Rows.Count - 1
            If (data.Rows.Item(i).Item("tipo").Equals("individual")) Then
                valor = valor + CType(data.Rows.Item(i).Item("obtenido"), Double)
                suma = suma + CType(data.Rows.Item(i).Item("porcentaje"), Double)
            End If
        Next
        datos.Add(valor)
        datos.Add(suma)
        Return datos
    End Function

    Private Function rendimientoGeneral() As ArrayList
        Dim datos As New ArrayList
        Dim valor As Double = 0
        Dim suma As Double = 0
        Dim i As Integer
        For i = 0 To data.Rows.Count - 1
            valor = valor + CType(data.Rows.Item(i).Item("valor"), Double)
        Next
        If data.Rows.Count > 0 Then
            valor = valor / data.Rows.Count
        End If
        If valor > 100 Then
            valor = 100
        End If
        datos.Add(valor)
        datos.Add(100)
        Return datos
    End Function

    Private Function rendimientoIndividual() As ArrayList
        Dim datos As New ArrayList
        Dim valor As Double = 0
        Dim suma As Double = 0
        Dim i, j As Integer
        For i = 0 To data.Rows.Count - 1
            If (data.Rows.Item(i).Item("tipo").Equals("individual")) Then
                valor = valor + CType(data.Rows.Item(i).Item("valor"), Double)
                j = j + 1
            End If
        Next
        If j > 0 Then
            valor = valor / j
        End If
        datos.Add(valor)
        datos.Add(100)
        Return datos
    End Function

    Private Function puntajeGeneralMaximo() As ArrayList
        Dim datos As New ArrayList
        Dim valor As Double = 0
        Dim suma As Double = 0
        Dim complemento As Double = 0
        Dim i As Integer
        For i = 0 To data.Rows.Count - 1
            valor = valor + CType(data.Rows.Item(i).Item("obtenido"), Double)
            suma = suma + CType(data.Rows.Item(i).Item("porcentaje"), Double)
        Next
        complemento = 100 - suma
        valor = valor + complemento
        datos.Add(valor)
        datos.Add(100)
        Return datos
    End Function

    Private Function puntajeIndividualMaximo() As ArrayList
        Dim datos As New ArrayList
        Dim valor As Double = 0
        Dim suma As Double = 0
        Dim complemento As Double = 0
        Dim i, j As Integer
        For i = 0 To data.Rows.Count - 1
            If (data.Rows.Item(i).Item("tipo").Equals("individual")) Then
                valor = valor + CType(data.Rows.Item(i).Item("obtenido"), Double)
                suma = suma + CType(data.Rows.Item(i).Item("porcentaje"), Double)
                j = j + 1
            End If
        Next
        complemento = 100 - suma
        valor = valor + complemento
        datos.Add(valor)
        datos.Add(100)
        Return datos
    End Function

    Private Sub obtenerDesglose()
        data = New accesoBD.TripleCDataSetTableAdapters.selectNotasTableAdapter().GetData(Me.idS)
        Me.calcularPorcentaje()
    End Sub

    Public Function verDesglose() As DataTable
        Me.obtenerDesglose()
        Return data
    End Function

    Private Sub calcularPorcentaje()
        Dim porcen As Double
        Dim valor As Double
        Dim i As Integer
        Dim objN As Evaluacion
        For i = 0 To data.Rows.Count - 1
            objN = New Evaluacion()
            porcen = CType(data.Rows.Item(i).Item(4), Double)
            valor = CType(data.Rows.Item(i).Item(1), Double)
            data.Rows.Item(i).Item("obtenido") = objN.calcularPorcentaje(porcen, valor)
        Next
        'tr.nombre, tn.valor, tn.idRubro, tr.tipo, tr.porcentaje, obtenido
        '    0         1           2          3          4            5
    End Sub

    Private Sub obtenerExamen(ByVal pidr As Integer, ByVal pnota As String)
        Dim da As DataTable = New accesoBD.TripleCDataSetTableAdapters.obtenerEstudiante_EvaluacionTableAdapter().GetData(Me.idS, pidr)
        If da.Rows.Count > 0 Then
            Dim idEva As Integer = CInt(da.Rows.Item(0).Item(0))
            Dim idRubro As Integer = CInt(da.Rows.Item(0).Item(2))
            'tn.idN, tn.valor, tn.idRubro, tn.idE
            '   0       1          2         3
            Me.objEvaluacion = New Evaluacion(idEva, pnota, idRubro, Me.idS)
        End If
    End Sub

    Public Sub asignarNota(ByVal pidR As Integer, ByVal pnota As String)
        Me.obtenerExamen(pidR, pnota)
        If Me.objEvaluacion IsNot Nothing Then
            Me.objEvaluacion.insertese(1)
        End If
    End Sub

    Public Sub modificarNota(ByVal pidG As Integer, ByVal pidR As Integer, ByVal pnota As String)
        Me.obtenerExamen(pidR, pnota)
        Dim da As DataTable = New accesoBD.TripleCDataSetTableAdapters.selectEstudianteTableAdapter().GetData(pidG, Me.idS)
        Me.idE = da.Rows.Item(0).Item(6)
        'Throw New ArgumentException("Pasa" & idE)
        Me.objEvaluacion.actualizar(Me.idE)
    End Sub

    Public Function obtenerListaEvaluada(ByVal objR As Rubro, ByVal pidG As Integer, ByVal pidR As Integer) As DataTable
        Dim dt As New DataTable
        If (objR.getTipo().Equals("individual")) Then
            dt = New accesoBD.TripleCDataSetTableAdapters.obtenerEstudiantes_EvaluacionTableAdapter().GetData(pidG, pidR)
        ElseIf (objR.getTipo().Equals("grupal")) Then
            dt = New accesoBD.TripleCDataSetTableAdapters.obtenerEquipos_EvaluacionTableAdapter().GetData(pidG, pidR)
        End If
        Return dt
    End Function

    Private Sub obtenerExamenEquipo(ByVal pidr As Integer, ByVal pnota As String)
        Dim da As DataTable = New accesoBD.TripleCDataSetTableAdapters.obtenerEquipo_EvaluacionTableAdapter().GetData(Me.idS, pidr)
        Dim idEva As Integer = CInt(da.Rows.Item(0).Item(0))
        Dim idRubro As Integer = CInt(da.Rows.Item(0).Item(2))
        'tn.idN, tn.valor, tn.idRubro, tn.idE
        '   0       1          2         3
        Me.objEvaluacion = New Evaluacion(idEva, pnota, idRubro, Me.idS)
        'Throw New ArgumentException("Pasa")
    End Sub

    Public Sub asignarNotaEquipo(ByVal pidR As Integer, ByVal pnota As String)
        Me.obtenerExamenEquipo(pidR, pnota)
        Me.objEvaluacion.insertese(2)
        Me.asignarNotaEstudiantes(pidR, pnota)
    End Sub

    Private Sub asignarNotaEstudiantes(ByVal pidR As Integer, ByVal pnota As String)
        Dim da As DataTable = New accesoBD.TripleCDataSetTableAdapters.selectEstudiantes_EquipoTableAdapter().GetData(Me.idS)
        For i As Integer = 0 To da.Rows.Count - 1
            Dim id As Integer = CInt(da.Rows.Item(i).Item(4))
            'Throw New ArgumentException(id)
            'te.nombre, te.apellidos, te.carne, te.email, te.idE, te.idGrupo
            '   0            1            2         3        4         5
            Dim objE As New Estudiante(id)
            objE.asignarNota(pidR, pnota, Me.idS)
        Next
    End Sub


    Public Sub darNota(ByVal pidR As Integer, ByVal pnota As String)
        Dim dt, dt1 As New DataTable
        dt = New accesoBD.TripleCDataSetTableAdapters.obtenerEstudiante_EvaluacionTableAdapter().GetData(Me.idS, pidR)
        If dt.Rows.Count = 0 Then
            'Dim ds As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
            'ds.crearExamen(Me.idS, pidR, pnota)
            Me.objEvaluacion = New Evaluacion(pnota, pidR, Me.idS)
            Me.objEvaluacion.crearNota(1)
        End If
    End Sub

    Public Sub darNotaEquipo(ByVal pidR As Integer, ByVal pnota As String, ByVal pidG As Integer)
        Dim dt As New DataTable
        dt = New accesoBD.TripleCDataSetTableAdapters.obtenerEstudiante_EvaluacionTableAdapter().GetData(Me.idS, pidR)
        If dt.Rows.Count = 0 Then
            Me.crearExamenEquipo(pidR, pnota, pidG)
        End If
    End Sub

    Private Sub crearExamenEstudiantes(ByVal pidR As Integer, ByVal pnota As Integer, ByVal pidG As Integer)
        Dim idE As Integer
        Dim dt As New DataTable
        Dim dtE As DataTable = New accesoBD.TripleCDataSetTableAdapters.selectEstudiantes_EquipoTableAdapter().GetData(Me.idS)
        Dim ds As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
        For i As Integer = 0 To dtE.Rows.Count - 1
            idE = dtE.Rows.Item(i).Item(4)
            Me.objEvaluacion = New Evaluacion(pnota, pidR, idE)
            Me.objEvaluacion.crearNota(1)
        Next
    End Sub

    Private Sub crearExamenEquipo(ByVal pidR As Integer, ByVal pnota As String, ByVal pidG As Integer)
        Dim dt As New DataTable
        Dim objEva As New Evaluacion(pnota, pidR, Me.idS)
        objEva.crearNota(2)
        Me.crearExamenEstudiantes(pidR, pnota, pidG)
        'Throw New ArgumentException("Pasa")
    End Sub

    Public Sub eliminarNota(ByVal pidR As Integer, ByVal pidN As Integer, ByVal pidG As Integer, ByVal objR As Rubro)
        Dim ds As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
        If (objR.getTipo().Equals("individual")) Then
            ds.deleteNota(pidN)
        ElseIf (objR.getTipo().Equals("grupal")) Then
            Dim i As Integer
            Dim dt As DataTable = New accesoBD.TripleCDataSetTableAdapters.obtenerEstudiantes_EvaluacionTableAdapter().GetData(pidG, pidR)
            For i = 0 To dt.Rows.Count - 1
                Dim idN As Integer = dt.Rows.Item(i).Item(3)
                ds.deleteNota(idN)
            Next
            ds.deleteNotaEquipo(pidN)
        End If
    End Sub

End Class
